package models.persistence;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import enumerators.ItemType;
import enumerators.Quantity;
import models.domain.Address;
import models.domain.Bill;
import models.domain.BillableLineItem;
import models.domain.Call;
import models.domain.Contract;
import models.domain.ContractItem;
import models.domain.Customer;
import models.domain.Data;
import models.domain.Payment;
import models.domain.SMS;
import play.db.jpa.Transactional;

public class Initializer {

	@Transactional
	public void eraseData() {
		EntityManager em = JPAUtil.getCurrentEntityManager();

		// Query query = em.createNativeQuery("delete from Customer");
		// query.executeUpdate();
		// query = em.createNativeQuery("delete from items");
		// query.executeUpdate();
		// query = em.createNativeQuery("delete from bookauthors");
		// query.executeUpdate();
		// query = em.createNativeQuery("delete from books");
		// query.executeUpdate();
		// query = em.createNativeQuery("delete from publishers");
		// query.executeUpdate();
		// query = em.createNativeQuery("delete from authors");
		// query.executeUpdate();
		// query = em.createNativeQuery("delete from borrowers");
		// query.executeUpdate();
		// query = em.createNativeQuery("delete from borrowercategories");
		// query.executeUpdate();
	}

	@Transactional
	public void CreateFullSchema() {
		// eraseData();
		models.domain.User user = new models.domain.User("test", "test", false);
		Address address = new Address("street", "11", "12345");
		// New Customer
		Customer customer = new Customer("123415123", "elias", "karditsis",
				"mail@mail.com", "X12314", address, user, null);

		// Creating contract Items
		ContractItem contractItemData = new ContractItem();
		contractItemData.setItemType(ItemType.Data);
		contractItemData.setQuantity(Quantity.MegaBytes);
		contractItemData.setRate(500);

		ContractItem contractItemSMS = new ContractItem();
		contractItemSMS.setItemType(ItemType.SMS);
		contractItemSMS.setQuantity(Quantity.Messages);
		contractItemSMS.setRate(500);

		ContractItem contractItemTalkTime = new ContractItem();
		contractItemTalkTime.setItemType(ItemType.Voice);
		contractItemTalkTime.setQuantity(Quantity.Minutes);
		contractItemTalkTime.setRate(500);

		List<ContractItem> contractItems = new ArrayList<ContractItem>();
		contractItems.add(contractItemData);
		contractItems.add(contractItemSMS);
		contractItems.add(contractItemTalkTime);

		Contract contract = new Contract();
		contract.setBill(null);
		contract.setContractEndDate(new Date(2016, 8, 20));
		contract.setContractStartDate(new Date(2014, 8, 20));
		contract.setContractItem(contractItems);

		// Adding contracts to customer
		List<Contract> contracts = new ArrayList<Contract>();
		List<Bill> bills = new ArrayList<Bill>();

		// Creating a bill
		Bill bill = new Bill();
		bill.setCharge(50);
		bill.setUsageFrom(new Date(2014, 8, 20));
		bill.setUsageTo(new Date(2014, 9, 20));
		bill.setIssueDate(new Date(2014, 9, 25));
		bill.setDueDate(new Date(2014, 10, 20));

		// Create Payment
		Payment payment = new Payment();
		payment.setDateTime(new Date(2014, 9, 27));

		bills.add(bill);

		contract.setBill(bills);
		bill.setPayment(payment);
		contracts.add(contract);
		customer.setContracts(contracts);

		// Create billLineItems
		BillableLineItem billableLineItem = new BillableLineItem();
		billableLineItem.setDateTime(new Date(2014, 8, 26));

		Call call = new Call();
		call.setDestination("1234567890");
		call.setDuration(new Timestamp(4000));

		SMS sms = new SMS();
		sms.setDestination("9876543210");

		Data data = new Data();
		data.setAmount((float) 500);

		billableLineItem.setData(data);
		billableLineItem.setSms(sms);
		billableLineItem.setCall(call);

		EntityManager em = JPAUtil.getCurrentEntityManager();
		em.persist(customer);
		
		
		

		// em.persist(contract);

		// em.persist(contractItemData);

	}

}
